package com.example.cdctest.managecontroller;

import com.example.cdctest.pojo.Config;
import com.example.cdctest.pojo.Education;
import com.example.cdctest.pojo.Post;
import com.example.cdctest.pojo.TestPaper;
import com.example.cdctest.service.QuestionTypeService;
import com.example.cdctest.service.UserInfoService;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/manage/user-info")
public class ManageUserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private QuestionTypeService questionTypeService;

    //转发到用户管理的页面
    @GetMapping("user-info-list")
    public void userInfoList(){}

    //根据查询条件，查出对应的用户信息
    @RequestMapping("user-info-list-page")
    public ModelAndView getUserInfoPage(Integer tai, Date logmin, Date logmax, String text, Integer pageNum, HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        if(pageNum==null) pageNum=1;
        Integer pageSize=10;
        ServletContext servletContext=request.getSession().getServletContext();
        List<Config> configList=(List<Config>)servletContext.getAttribute("configs");
        for (Config config:configList
             ) {
            if("manage_user_info_count".equals(config.getKey())){
                pageSize=Integer.parseInt(config.getValue());
            }
        }
        modelAndView.addObject("pageInfo",userInfoService.findAllUserInfo(tai,logmin,logmax,text,pageNum,pageSize));
        modelAndView.addObject("userInfoCount",userInfoService.getUserInfoCount(tai,logmin,logmax,text,pageNum,pageSize));
        modelAndView.setViewName("/manage/user-info/user-info-data-list");
        return modelAndView;
    }

    //审核用户
    @RequestMapping("pass-user")
    @ResponseBody
    public void passUserInfo(Integer id,Integer userState){
        userInfoService.updateUserStatus(id,userState);
    }

    //禁用或者启用用户
    @RequestMapping("update-user")
    @ResponseBody
    public void updateUser(Integer id){
        userInfoService.updateUser(id);
    }

    //根据用户的id,获取用户的所有考试信息
    @RequestMapping("test-pager")
    public ModelAndView testPager(Integer userId){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("testPapers",userInfoService.selectUserTestPapers(userId));
        modelAndView.addObject("testPaperCount",userInfoService.getUserTestPaperCount(userId));
        modelAndView.setViewName("/manage/user-info/test-pager");
        return modelAndView;
    }

    //从excel表格中批量导入用户信息
    @RequestMapping("batch-import-user")
    public String batchImportUser(@RequestParam("user_File") MultipartFile userFile, Model model){
        Boolean flag=false;
        String name=userFile.getOriginalFilename();
        try{
            flag=userInfoService.userInfoExcel(name,userFile);
        }catch (Exception e){
            e.printStackTrace();
            return "/manage/inc/erro";
        }
        return "redirect:/manage/user-info/user-info-list";
    }

    //从数据库取出岗位信息,并转到岗位管理界面
    @RequestMapping("user-post-list")
    @ModelAttribute("postList")
    public List<Post> userPostList(){
        return userInfoService.findAllPost();
    }

    //转到添加岗位页面
    @GetMapping("post-add")
    public void postAdd(){ }

    //添加岗位信息
    @RequestMapping("add-post")
    @ResponseBody
    public void addPost(Post post){
        userInfoService.addPost(post);
    }

    //修改岗位的状态信息
    @RequestMapping("delete-post")
    @ResponseBody
    public void deletePost(Integer id){
        userInfoService.updatePostEnable(id);
        questionTypeService.clearCache();
    }

    //修改岗位排序
    @RequestMapping("update-post-sort")
    @ResponseBody
    public void updatePostSort(Post post){
        userInfoService.updatePostSort(post);
        questionTypeService.clearCache();
    }

    //从数据库取出学历信息,并转到学历管理界面
    @RequestMapping("user-education-list")
    @ModelAttribute("educationList")
    public List<Education> userEducationList(){
        return userInfoService.findAllEducation();
    }

    //转到学历添加页面上
    @GetMapping("education-add")
    public void educationAdd(){}

    //添加学历信息
    @RequestMapping("add-education")
    @ResponseBody
    public void addEducation(Education education){
        userInfoService.addEducation(education);
    }

    //修改岗位的状态信息
    @RequestMapping("delete-education")
    @ResponseBody
    public void deleteEducation(Integer id){
       userInfoService.updateEducationEnable(id);
        questionTypeService.clearCache();
    }

    //修改学历排序
    @RequestMapping("update-education-sort")
    @ResponseBody
    public void updateEducationSort(Education education){
       userInfoService.updateEducationSort(education);
        questionTypeService.clearCache();
    }
}
